﻿using Dispatcher.Core.DingTalk;
using Dispatcher.Core.DingTalk.Models;
using Dispatcher.Data.Entities.MySql;
using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Utility;

namespace Dispatcher.Data
{
    /// <summary>
    /// 
    /// </summary>
    [AutoResolve]
    public class DingTalkProvider : EntityDataProvider<DispatcherContext>, IDingTalkProvider
    {

        public void SaveMessageResult(DingTalkSendRequest request, DingTalkSendResponse response)
        {
            var dataItem = new DingtalkSent
            {
                MessageId = response.MessageId,
                AccountName = request == null ? string.Empty : request.AccountName,
                TemplateName = request == null ? string.Empty : request.TemplateName,
                Data = request == null ? string.Empty : request.Data.ToJsonString(),
                Receiver = request == null ? string.Empty : request.Receiver.ToJsonString(),
                Title = request == null ? string.Empty : request.Title,
                Url = request == null ? string.Empty : request.MessageUrl,
                PictureUrl = request == null ? string.Empty : request.Image,
                Response = response == null ? string.Empty : response.OrignalResponse,
                Body = request == null ? string.Empty : request.Body,
                Result = response == null ? string.Empty : response.ResultJson,
                SendTime = response == null ? DateTime.Now : response.SendTime,
                TaskId = response.TaskId
            };
            this.Insert(dataItem);
        }

    }
}
